package com.sky.controller.setmeal;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author iTaotao
 * @version 1.0.0
 * @ClassName SetMealController.java
 * @Description 套餐控制器
 * @createTime 2023年07月28日 17:36:00
 */
@Slf4j
@RestController
@RequestMapping("/admin/setmeal")
public class SetMealController {
    @Autowired
    private SetMealService setMealService;

    /**
     * 选择页面
     *
     * @param setmealPageQueryDTO setmeal页面查询dto
     * @return {@link Result}<{@link PageResult}>
     */
    @GetMapping("/page")
    public Result<PageResult> selectPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("分页 {}", setmealPageQueryDTO);
        PageResult pageResult = setMealService.selectPage(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 创建
     *
     * @param setmealDTO setmeal dto
     * @return {@link Result}<{@link ?}>
     */
    @PostMapping
    @CacheEvict(cacheNames = "setmeals" , allEntries = true)
    public Result<?> create(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增{}", setmealDTO);
        setMealService.create(setmealDTO);
        return Result.success();
    }

    /**
     * 更新状态通过id
     *
     * @param id     id
     * @param status 状态
     * @return {@link Result}<{@link ?}>
     */
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmeals" , allEntries = true)
    public Result<?> updateStatusById(Long id, @PathVariable Integer status) {
        log.info("id:{} , 状态:{}", id, status);
        setMealService.updateStatusById(id, status);
        return Result.success();
    }

    /**
     * 删除套餐id
     *
     * @param ids id
     * @return {@link Result}<{@link ?}>
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "setmeals" , allEntries = true)
    public Result<?> removeSetMealById(@RequestParam List<Long> ids) {
        log.info("id:{}", ids);
        setMealService.removeSetMealById(ids);
        return Result.success();
    }

    /**
     * 选择套餐id
     *
     * @param id id
     * @return {@link Result}<{@link SetmealVO}>
     */
    @GetMapping("/{id}")
    public Result<SetmealVO> selectSetMealById(@PathVariable Long id) {
        log.info("id:{}", id);
        SetmealVO setmealVO = setMealService.selectSetMealById(id);
        return Result.success(setmealVO);


    }

    /**
     * 更新套餐
     *
     * @param setmealDTO setmeal dto
     * @return {@link Result}<{@link ?}>
     */
    @PutMapping
    @CacheEvict(cacheNames = "setmeals" , allEntries = true)
    public Result<?> updateSetMeal(@RequestBody SetmealDTO setmealDTO){
        setMealService.updateSetMeal(setmealDTO);
        return Result.success();
    }


}
